package com.example.myapplication6;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;



public class MainActivity extends AppCompatActivity {
    private static final String TAG = "TAG";

    ServiceConnection mServiceConnection;
    CalcService mCalcService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button buttonStart=(Button)findViewById(R.id.Button_start);
        Button buttonEnd=(Button)findViewById(R.id.Button_destroy);
        Button buttonAdd=(Button)findViewById(R.id.Button_add);
        Button buttonSub=(Button)findViewById(R.id.Button_subtract);
        Button buttonMul=(Button)findViewById(R.id.Button_multiply);
        Button buttonDiv=(Button)findViewById(R.id.Button_divide);
        buttonStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onButtonStartServiceClick(view);
            }
        });

        buttonEnd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onButtonStopServiceClick(view);
            }
        });

        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onButtonAddClick(view);
            }
        });

        buttonSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onButtonSubClick(view);
            }
        });


        mServiceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                Log.v(TAG, "onServiceConnected");
                mCalcService = ((CalcService.LocalBinder) iBinder).getService();
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                Log.v(TAG, "onServiceDisconnected");
            }
        };

    }

    public void onButtonStartServiceClick(View view) {
        if (mCalcService != null){
            Toast.makeText(MainActivity.this, "服务已开启",Toast.LENGTH_SHORT).show();
        }
        else{
            Intent intent = new Intent(MainActivity.this, CalcService.class);
            bindService(intent, mServiceConnection, Service.BIND_AUTO_CREATE);
            Toast.makeText(MainActivity.this, "服务开启",Toast.LENGTH_SHORT).show();
        }
    }

    public void onButtonStopServiceClick(View view) {
        if (mCalcService != null){
            Toast.makeText(MainActivity.this, "服务关闭",Toast.LENGTH_SHORT).show();
            unbindService(mServiceConnection);
            mCalcService=null;
        }
        else{
            Toast.makeText(MainActivity.this, "服务尚未开启",Toast.LENGTH_SHORT).show();
        }
    }

    public void onButtonAddClick(View view) {
        if (mCalcService != null){
            Toast.makeText(MainActivity.this, "Using service add:" + mCalcService.add(10, 5), Toast.LENGTH_LONG).show();
        }
        else{
            Toast.makeText(MainActivity.this, "服务尚未开启",Toast.LENGTH_SHORT).show();
        }
    }

    public void onButtonSubClick(View view) {
        if (mCalcService != null){
            Toast.makeText(MainActivity.this, "Using service sub:" + mCalcService.subtract(10, 5), Toast.LENGTH_LONG).show();
        }
        else{
            Toast.makeText(MainActivity.this, "服务尚未开启",Toast.LENGTH_SHORT).show();
        }
    }

}